package core.util;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * @author songduk park
 * 
 */
public abstract class AbstractWeb {

	public int getIntParameter(HttpServletRequest request, String name) {
		String temp = request.getParameter(name);

		int result = 0;
		try {
			result = Integer.parseInt(temp);
		} catch (Exception e) {
		}

		return result;
	}

	public User getUser(HttpServletRequest request) {
		Object obj = request.getAttribute(User.user);

		if (obj == null)
			return null;

		return (User) obj;
	}

	public Period getPeriod(HttpServletRequest request,
			String defaultTypeOfPeriod, Period period) {
		period.this_year = getIntParameter(request, period.lable_thie_year);
		period.this_month = 0;
		period.this_week = 0;

		if (period.this_year == 0) { // nothing from web
			// check session
			Object obj = request.getSession().getAttribute(period.lable_period);

			if (obj != null) {
				period = (Period) obj;
			} else { // set default
				period.this_year = DateUtil.getYear();
				period.this_month = DateUtil.getMonth() - 1;
				if (period.this_month == 0) {
					period.this_month = 12;
					period.this_year = period.this_year - 1;
				}
				period.this_week = DateUtil.getWeek();

				period.type_period = defaultTypeOfPeriod;
			}
		} else {
			period.this_month = getIntParameter(request,
					period.lable_thie_month);
			period.this_week = getIntParameter(request, period.lable_thie_week);

			period.type_period = request.getParameter(period.lable_type_period);

			// save it to session
			request.getSession().setAttribute(period.lable_period, period);
		}

		request.setAttribute(period.lable_type_period, period.type_period);

		request.setAttribute(period.lable_thie_year, period.this_year);
		request.setAttribute(period.lable_thie_month, period.this_month);
		request.setAttribute(period.lable_thie_week, period.this_week);

		return period;
	}

	public void downloadFile(HttpServletRequest request,
			HttpServletResponse response, File file) {
		String fileName = file.getName().replace(' ', '_');

		response.setContentType("application/csv");
		response.setHeader("Content-Disposition", "attachment;filename="
				+ fileName);

		File dir = file.getParentFile();

		FileInputStream fileIn;
		try {
			fileIn = new FileInputStream(file);
			ServletOutputStream out = response.getOutputStream();

			int buf = 4096;
			byte[] outputByte = new byte[buf];

			int length = -1;
			while ((length = fileIn.read(outputByte)) != -1) {
				out.write(outputByte, 0, length);
			}
			fileIn.close();
			out.close();

			System.out.println("complete download");
			try {
				response.flushBuffer();
			} catch (Exception e) {
				e.printStackTrace();
			}

		} catch (FileNotFoundException e1) {
			e1.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}

		DeleteOldFiles deleteOldFiles = new DeleteOldFiles(14, dir);
		deleteOldFiles.delete();
	}

	protected Page makePage(HttpServletRequest request, int rowSize,
			int rowsAPage) {
		int pageRequested = getIntParameter(request, Page.PAGE_REQUESTED);

		Page page = new Page(rowSize, rowsAPage, pageRequested);
		return page;
	}

	protected void putPageToMap(Page page, StringHashMap map) {
		map.put("row_start", page.startingRow);
		map.put("row_end", page.endingRow);
	}

}
